﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using Common;

namespace Service.BusinessLogic
{
    public class ExpressionParserConfig
    {
        protected static IDictionary<String, FunctionInfo> _customFunctionInfos = null;
        private static Object _lockCustomFunctionInfos = new Object();
        public static IDictionary<String, FunctionInfo> CustomFunctionInfos
        {
            get
            {
                if (_customFunctionInfos != null)
                    return _customFunctionInfos;

                lock (_lockCustomFunctionInfos)
                {
                    if (_customFunctionInfos == null)
                        Init();
                }

                return _customFunctionInfos;
            }
        }

        public static void Init()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(ExpressionParserConfigStorage));
            TextReader tr = new StreamReader(AppDomain.CurrentDomain.BaseDirectory
                + "Customization\\Config\\ExpressionParserConfiguration.xml");
            ExpressionParserConfigStorage cfg = serializer.Deserialize(tr) as ExpressionParserConfigStorage;

            _customFunctionInfos = new Dictionary<String, FunctionInfo>();

            foreach (NamedFunctionInfo nfi in cfg.FunctionInfos)
            {
                _customFunctionInfos.Add(nfi.Name, nfi.FunctionInfo);
            }
        }

        public static FunctionInfo GetFunctionInfo(String functionCode)
        {
            FunctionInfo result = CustomFunctionInfos[functionCode];
            return result;
        }
    }
}
